//
//  AppDelegate.m
//  MyProject
//
//  Created by yun on 2018/3/21.
//  Copyright © 2018年 yun. All rights reserved.
//

#import "AppDelegate.h"
#import "BackgroundDownloadManager.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    /** 3dtouch —— 主屏交互(Home Screen Interaction)
     - (instancetype)initWithType:(NSString *)type localizedTitle:(NSString *)localizedTitle localizedSubtitle:(nullable NSString *)localizedSubtitle icon:(nullable UIApplicationShortcutIcon *)icon userInfo:(nullable NSDictionary *)userInfo
     type：唯一标识符
     localizedTitle：主标题
     localizedSubtitle：副标题
     icon：图标(分为系统和自定义)
     系统：+ (instancetype)iconWithType:(UIApplicationShortcutIconType)type;
     
     自定义：+ (instancetype)iconWithTemplateImageName:(NSString *)templateImageName;
     Icons should be square, single color, and 35x35 points
     
     userInfo：携带的信息
     
     主屏交互最多显示4栏，上线会增加一个分享栏
     */
    
    if (@available(iOS 9.1, *)) {
        UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"3dtouch.home" localizedTitle:@"HOME" localizedSubtitle:@"home" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome] userInfo:nil];
        
        UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc] initWithType:@"3dtouch.compose" localizedTitle:@"COMPOSE" localizedSubtitle:@"compose" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCompose] userInfo:nil];
        
        [UIApplication sharedApplication].shortcutItems = @[item1,item2];
    }
    
    return YES;
}

// 主屏交互监听
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler  API_AVAILABLE(ios(9.0)){
    
    //根据标识符进行相应的操作
    NSLog(@"name:%@\ntype:%@", shortcutItem.localizedTitle, shortcutItem.type);
}

/*
 告诉委托,URL会话相关的事件正在等待处理。
 identifier：session的标识符
 completionHandler：调用它，可以让系统知道app的UI更新
 
 当与NSURLSession对象关联的所有后台传输完成时，应用程序调用此方法，无论它们是否成功完成或结果错误。如果一次或多次传输需要身份验证，该应用程序还会调用此方法。
 
 使用此方法重新连接任何URL Sessions并更新应用的用户界面
 
 如果某个URL会话在您的应用程序未运行时完成其工作，系统将在后台启动您的应用程序，以便它可以处理该事件。在这种情况下，使用提供的标识符来创建一个新的NSURLSessionConfiguration和NSURLSession对象。您必须像开始上传或下载时一样配置NSURLSessionConfiguration对象的其他选项。在创建和配置新的NSURLSession对象后，该对象会调用相应的委托方法来处理事件。
 
 如果您的应用程序已经具有指定标识符的会话对象并且正在运行或挂起，则不需要使用此方法创建新的会话对象。挂起的应用程序被移入后台。只要应用程序再次运行，带有标识符的NSURLSession对象就会接收事件并正常处理它们。
 
 在启动时，如果上传或下载正在进行但尚未完成，应用程序不会调用此方法。如果要在应用程序的用户界面中显示这些传输的当前进度，则必须自己重新创建会话对象。在这种情况下，请持久化缓存标识符值并使用它来重新创建会话对象。
 
 如果iOS应用程序被系统终止并重新启动，则应用程序可以使用相同的标识符创建新的配置对象和会话，并检索终止时正在进行的传输状态。此行为仅适用于系统正常终止应用程序。如果用户从多任务处理屏幕终止应用程序，系统将取消所有会话的后台传输。另外，系统不会自动重新启动用户强制退出的应用程序。用户必须在传输重新开始之前明确重新启动应用程序。
 
 
 */
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)(void))completionHandler{
    NSURLSession *session = [BackgroundDownloadManager backgroundSession];
    self.bgSessionCompletionHandler = completionHandler;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    
}



@end
